#ifndef __VSDL_GAMENGINE_H__
#define __VSDL_GAMENGINE_H__

#include <vector>
#include <SDL/SDL.h>
#include "VSDL_system.h"
#include "VSDL_event.h"
#include "VSDL_timer.h"

class VSDL_GameState;

class VSDL_GameEngine : public VSDL_System
{
private:
	int frameRate;
	unsigned int totalFrames;
	SDL_Event event;
	VSDL_Timer fps;
	bool running;
	std::vector<VSDL_GameState *> states;
	void HandleEvents();
	void Update();
	void Draw();
public:
	VSDL_GameEngine();
	VSDL_GameEngine(char *windowCaption);
	VSDL_GameEngine(int screenWidth, int screenHeight, char *windowCaption, int bpp, bool fullScreen);
	void SetFrameRate(int frameRate);
	int GetFrameRate();
	void IncFrameRate();
	void DecFrameRate();
	void AddFrameRate(int value);
	void PushState(VSDL_GameState *state);
	void PopState();
	void ChangeState(VSDL_GameState *state);
	VSDL_GameState *CurrentState();
	virtual void CapFrames(){
		while (fps.GetTicks() < 1000 / frameRate){
			//Check for SDL_Quit
			SDL_Event quitWatcher;
			
			SDL_PumpEvents();
			SDL_PeepEvents(&quitWatcher, 1, SDL_PEEKEVENT, SDL_EVENTMASK(SDL_QUIT));
			
			if (quitWatcher.type == SDL_QUIT){
				running = false;
			}
				
		}
	};
	virtual void Run(); //If there's a better way to implement main loop
	void Quit(){running = false;};
	void CleanUp();
};

#endif /*__VSDL_GAMENGINE_H__*/
